/*
*/

#include <sensor.h>
#include <i2c.h>
#include <timer.h>

#define	ACC_CLICKCFG	0x38
#define	ACC_CLICKSRC	0x39
#define	ACC_CLICKTHS	0x3A
#define	ACC_CTRL1	0x20
#define	ACC_CTRL2	0x21
#define	ACC_CTRL3	0x22
#define	ACC_CTRL4	0x23
#define	ACC_CTRL5	0x24
#define	ACC_CTRL6	0x25
#define	ACC_FIFOCTRL	0x2E
#define	ACC_FIFOSRC	0x2F
#define	ACC_INTCFG	0x30
#define	ACC_INTCOUNTER	0x0E
#define	ACC_INTDURATION	0x33
#define	ACC_INTSRC	0x31
#define	ACC_INTTHS	0x32
#define	ACC_OUTXL	0x28
#define	ACC_OUTXH	0x29
#define	ACC_OUTYL	0x2A
#define	ACC_OUTYH	0x2B
#define	ACC_OUTZL	0x2C
#define	ACC_OUTZH	0x2D
#define	ACC_REFERENCE	0x26
#define	ACC_STATUS2	0x27
#define	ACC_STATUSAUX	0x07
#define	ACC_TEMPCFG	0x1F
#define	ACC_TIMELIMIT	0x3B
#define	ACC_TIMELATENCY	0x3C
#define	ACC_TIMEWINDOW	0x3D
#define	ACC_WHOIAM	0x0F

#define	ACC_DEV		0x30
/* Private variables----------------------------------------------------------*/ 
extern volatile U8 Direction;
extern U8 TransmitData[];
extern volatile U8 ReceiveData[];
extern U8 WorRFlag,ACCorEEPFlag;
//static U8 I2C2_SLAVE_ADDRESS7;
extern U8 I2CSendLen,I2CSendCount;
extern U8 I2CRevLen,I2CRevCount;

static void WriteByte(U8 vol,U8 reg,U8 dev){
    U8	v;

    Delay(1);
    v=vol;
    I2cWrite(reg,dev,&v,1);
}

void GsRead(S16 *buf){
    I2cRead(0x00A8,buf,6);
}

void InitSensor(void){
    GPIO_InitTypeDef g;
    EXTI_InitTypeDef x;
    
    g.GPIO_Pin	=GPIO_Pin_0;
    g.GPIO_Speed=GPIO_Speed_50MHz;
    g.GPIO_Mode	=GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA,&g);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
    x.EXTI_Line	=EXTI_Line0;
    x.EXTI_Mode	=EXTI_Mode_Interrupt;
    x.EXTI_Trigger=EXTI_Trigger_Falling;
    x.EXTI_LineCmd=ENABLE;
    EXTI_Init(&x);

    WriteByte(0x27,ACC_CTRL1,ACC_DEV);
    WriteByte(0xf4,ACC_CTRL2,ACC_DEV);
    WriteByte(0x80,ACC_CTRL3,ACC_DEV);
    WriteByte(0x00,ACC_CTRL4,ACC_DEV);	
    WriteByte(0x08,ACC_CTRL5,ACC_DEV);
	WriteByte(0x02,ACC_CTRL6,ACC_DEV);
    WriteByte(0x95,ACC_CLICKCFG,ACC_DEV);
    WriteByte(0x10,ACC_CLICKTHS,ACC_DEV);
    WriteByte(0x7f,ACC_INTDURATION,ACC_DEV);
    WriteByte(0x48,ACC_TIMELIMIT,ACC_DEV);
    WriteByte(0x80,ACC_TEMPCFG,ACC_DEV);
}
